package zone.czh.woi.woim.client.prtocol.builder;

import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPipeline;
import lombok.Data;
import zone.czh.woi.base.util.Pair;
import zone.czh.woi.woim.base.channel.adapter.ProtocolAdapter;
import zone.czh.woi.woim.base.channel.constant.ChannelHandlerName;
import zone.czh.woi.woim.base.channel.protocol.builder.ProtocolBuilder;

import java.util.List;
import java.util.Map;

@Data
public class ClientProtocolBuilder implements ProtocolBuilder {
    Map<String,ProtocolAdapter> protocolAdapters;

    @Override
    public void build(ChannelPipeline pipeline, String protocolName) {
        if (protocolAdapters!=null){
            ProtocolAdapter adapter = protocolAdapters.get(protocolName);
            if (adapter==null){
                throw new RuntimeException("no adapter for protocol:"+protocolName);
            }
            pipeline.addBefore(ChannelHandlerName.PAYLOAD_HANDLER,protocolName,adapter);
            List<Pair<String, ChannelInboundHandler>> decoderPairs = adapter.getDecoders();
            List<Pair<String, ChannelOutboundHandler>> encoderPairs = adapter.getEncoders();
            if(decoderPairs!=null){
                //decoder
                for (int i=0;i<decoderPairs.size();++i){
                    ChannelInboundHandler decoder = decoderPairs.get(i).getValue();
                    String name = decoderPairs.get(i).getKey();
                    pipeline.addBefore(protocolName,name!=null?name:protocolName+"-decoder-"+i,decoder);
                }
            }
            if (encoderPairs!=null){
                //encoder
                for (int i=0;i<encoderPairs.size();++i){
                    ChannelOutboundHandler encoder = encoderPairs.get(i).getValue();
                    String name = encoderPairs.get(i).getKey();
                    pipeline.addBefore(ChannelHandlerName.PAYLOAD_ENCODER,name!=null?name:protocolName+"-encoder-"+i,encoder);
                }
            }
        }
    }
}
